<?php
Raise::load('core.RaiseUIControl');
Raise::load('core.RaiseValidator');
Raise::load('core.RaiseHtmlTag');
Raise::load('control.RaiseFieldLabel');

/**
 * RaiseFormField UIControl abstract class
 * represents a field on a form
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @abstract
 * @package Raise.Core.UI.Form
 * @since 1.2
 */
abstract class RaiseFormField extends RaiseUIControl {

    /**
     * The label that is associated 
     * @var RaiseFieldLabel
     */
    protected $label;

    /**
     * The validator in which this field will be validated with when changed
     * @var RaiseValidator
     */
    protected $validator;

    /**
     * The CSS class names
     * @var string
     */
    protected $cssClass;

    /**
     * The internal tag
     * @var RaiseHtmlTag
     */
    protected $tag;

    /**
     * Get or set the RaiseFieldLabel associated with this the form field
     * @param RaiseFieldLabel $l (optional) If passed in, the new value will be set.
     * @return RaiseFieldLabel
     */
    public function label($l = false){
        if(func_num_args() == 1){
            if(!RaiseVariable::isSubclass($l, 'RaiseFieldLabel') && $l !== null){
                throw new RaiseInvalidArgumentException('RaiseFormField::label() expecting RaiseFieldLabel or NULL as first parameter; ' . RaiseVariable::typeInfo($l) . ' was given instead.');
            }
            if($this->label != null){
                $this->label->field(null);
            }
            if($l != null){
                $l->field($this);
            }
            $this->label = $l;
        }
        return $this->label;
    }

    /**
     * Get or set the validator (or the name of the validator) in which this field will be validated with when changed
     * @param RaiseValidator|string $v (optional) If passed in, the new validator will be set.
     * @return RaiseValidator
     */
    public function validator($v = false){
        if(func_num_args() == 1){
            if(!RaiseVariable::isType($v, array('string', 'RaiseValidator'))){
                throw new RaiseInvalidArgumentException('RaiseFormField::validator() expecting first parameter to be of type string or RaiseValidator; ' . RaiseVariable::typeInfo($v) . ' was given instead.');
            }
            $this->validator = $v;
        }
        return $this->validator;
    }

    /**
     * Get or set the CSS class names of this form field.
     * @param string $c (optional) If passed in, the new value will be set.
     * @return string
     */
    public function cssClass($c = false){
        if(func_num_args() == 1){
            $this->cssClass = $c;
        }
        return $this->cssClass;
    }

    /**
     * Get or set the internal HTML tag
     * @param RaiseHtmlTag $t (optional) Set the internal HTML tag
     * @return RaiseHtmlTag
     */
    public function tag($t = null){
        if(func_num_args() == 1){
            $this->tag = $t;
        }
        return $this->tag;
    }

}
